package com.ulook.lookplanet.ui.matching.heart;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.qmuiteam.qmui.layout.QMUIFrameLayout;
import com.qmuiteam.qmui.skin.QMUISkinHelper;
import com.qmuiteam.qmui.skin.QMUISkinManager;
import com.qmuiteam.qmui.skin.QMUISkinValueBuilder;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.util.QMUIResHelper;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.popup.QMUIFullScreenPopup;
import com.qmuiteam.qmui.widget.popup.QMUIPopups;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.ulook.lookplanet.R;
import com.ulook.lookplanet.data.model.TPlanet;
import com.ulook.lookplanet.ui.notifications.friend.FriendActivity;
import com.ulook.lookplanet.ui.notifications.friend.FriendViewModel;
import com.ulook.lookplanet.widget.PlanetView;

public class HeartActivity extends AppCompatActivity {

    private HeartViewModel viewModel;
    private int index;
    private TextView qua;
    private QMUIRoundButton btA;
    private QMUIRoundButton btB;
    private TextView indexTx;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_heart);

        viewModel = new ViewModelProvider(this).get(HeartViewModel.class);
        viewModel.getToastText().observe(this, s -> {
            Toast.makeText(HeartActivity.this, s, Toast.LENGTH_SHORT).show();
        });
        viewModel.getPlanetInfo().observe(this, tPlanet -> {
            showPlanetInfo(tPlanet);
        });

        Intent intent = getIntent();
        int currUserId = intent.getIntExtra("currUserId", 0);
        if(currUserId == 0){
            Toast.makeText(this, "未登录", Toast.LENGTH_SHORT).show();
            finish();
        }
        viewModel.setCurrUserId(currUserId);


        initTopBar();
        initData();

    }

    private void initTopBar(){
        QMUITopBarLayout topBar = findViewById(R.id.heart_bar);
        topBar.setTitle("心灵测试");
        topBar.addLeftBackImageButton().setOnClickListener(view -> {
            finish();
        });
    }

    private void initData(){
        qua = findViewById(R.id.heart_qua);
        btA = findViewById(R.id.heart_opt_a);
        btB = findViewById(R.id.heart_opt_b);
        indexTx = findViewById(R.id.heart_index);

        index = 0;
        nextAsk();

        btA.setOnClickListener(view -> {
            viewModel.addOpt(viewModel.getAsks(index).getOptA());
            index++;
            if(index == HeartViewModel.ASK_SIZE){
                viewModel.calculate();

            }else {
                nextAsk();
            }

        });
        btB.setOnClickListener(view -> {
            viewModel.addOpt(viewModel.getAsks(index).getOptB());
            index++;
            if(index == HeartViewModel.ASK_SIZE){
                viewModel.calculate();
            }else {
                nextAsk();
            }
        });
    }

    /**
     * 下一题
     */
    private void nextAsk(){
        if(index == HeartViewModel.ASK_SIZE){
            btA.setClickable(false);
            btB.setClickable(false);
            return;
        }
        qua.setText(viewModel.getAsks(index).getQue());
        btA.setText(viewModel.getAsks(index).getA());
        btB.setText(viewModel.getAsks(index).getB());
        indexTx.setText(index+"");
    }

    /**
     * 弹框显示则是结果
     */
    private void showPlanetInfo(TPlanet planet){
        QMUISkinValueBuilder builder = QMUISkinValueBuilder.acquire();
        QMUIFrameLayout frameLayout = new QMUIFrameLayout(this);
        frameLayout.setBackground(QMUIResHelper.getAttrDrawable(this, R.attr.qmui_skin_support_popup_bg));
        builder.background(R.attr.qmui_skin_support_popup_bg);
        QMUISkinHelper.setSkinValue(frameLayout, builder);
        frameLayout.setRadius(QMUIDisplayHelper.dp2px(this, 12));
        int padding = QMUIDisplayHelper.dp2px(this, 20);
        frameLayout.setPadding(padding, padding, padding, padding);

        builder.release();

        PlanetView planetView = new PlanetView(this);
        planetView.setWidgetData(planet, viewModel.getCurrUserId());

        frameLayout.addView(planetView);

        QMUIPopups.fullScreenPopup(this)
                .addView(frameLayout)
                .closeBtn(true)
                .skinManager(QMUISkinManager.defaultInstance(this))
                .onBlankClick(new QMUIFullScreenPopup.OnBlankClickListener() {
                    @Override
                    public void onBlankClick(QMUIFullScreenPopup popup) {
                        //Toast.makeText(HeartActivity.this, "点击到空白区域", Toast.LENGTH_SHORT).show();
                    }
                })
                .onDismiss(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        finish();
                    }
                })
                .show(qua);
    }
}